/*
 * @Author: your name
 * @Date: 2021-12-03 14:00:44
 * @LastEditTime: 2021-12-04 08:13:22
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\admin\src\mixins\webScoket.js
 */
//webSocket混合

export default {
    data() {
        return {
            ws: null,
        };
    },
    mounted() {
        if (window.WebSocket) {
            //实例化出来对象
            this.ws = new WebSocket("ws://localhost:8181");
            this.ws.onopen = (e) => {
                console.log("链接成功");
                this.ws.send(
                    JSON.stringify({
                        value: this.value,
                        text: this.text,
                        tag: this.tag,
                        classify: this.classify,
                        // focus: this.info.focus,
                        // cover: this.info.cover,
                        // abstract: this.info.abstract,
                        // visit_pwd: this.info.visit_pwd,
                        // pay_check: this.info.pay_check,
                        // is_comment: this.info.is_comment,
                    })
                );
            };
            this.ws.onmessage = (e) => {
                let resData = JSON.parse(e.data);
                const {
                    title,
                    text,
                    tag,
                    classify,
                    focus,
                    cover,
                    abstract,
                    visit_pwd,
                    pay_check,
                    is_comment,
                } = resData;
                this.title = title;
                this.text = text;
                this.tag = tag;
                this.classify = classify;
                this.focus = focus;
                this.cover = cover;
                this.abstract = abstract;
                this.visit_pwd = visit_pwd;
                this.pay_check = pay_check;
                this.is_comment = is_comment;
            };
            this.ws.onerror = () => {
                console.log("出错了");
            };
            this.ws.onclose = () => {
                console.log("断开连接");
            };
        }
    },
    watch: {
        classify(newval, oldval) {
            if (newval !== oldval) {
                this.ws.send(
                    JSON.stringify({
                        classify: newval,
                        value: this.value,
                        text: this.text,
                        tag: this.tag,
                    })
                );
            }
        },
        text(newval, oldval) {
            if (newval !== oldval) {
                this.ws.send(
                    JSON.stringify({
                        text: newval,
                        tag: this.tag,
                        value: this.value,
                        classify: this.classify,
                    })
                );
            }
        },
        tag(newval, oldval) {
            if (newval !== oldval) {
                this.ws.send(
                    JSON.stringify({
                        tag: newval,
                        classify: this.classify,

                        value: this.value,
                        text: this.text,
                    })
                );
            }
        },
    },
};